package com.googlecode.openauth.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import com.googlecode.openauth.web.config.CommonProperty;
import com.googlecode.openauth.web.dao.IResourceDao;
import com.googlecode.openauth.web.dao.IRoleDao;
import com.googlecode.openauth.web.dao.IRoleResDao;
import com.googlecode.openauth.web.entity.ActionPageBean;
import com.googlecode.openauth.web.entity.ResourceInfo;
import com.googlecode.openauth.web.entity.RoleInfo;
import com.googlecode.openauth.web.entity.RoleResource;
import com.googlecode.openauth.web.entity.UserInfo;
import com.googlecode.openauth.web.util.Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ResourceAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1110764698528194377L;

	private int resId;
	private int[] resIds;
	private String resName;
	private int resType = 1;
	private String resDesc;
	private IResourceDao resourceDao;
	private IRoleResDao roleResDao;
	private List<ResourceInfo> list;

	private String oper = "list";

	private ActionPageBean<ResourceInfo> actionPageBean;
	private int currentPageNo = 1;
	private int prevPageNo = 1;
	private int nextPageNo = 1;
	private int totalCount = 0;
	private int pageNum = CommonProperty.PAGENUM;
	
	private static Logger logger = Logger.getLogger(ResourceAction.class);

	public String list() throws Exception {
		ResourceInfo resourceInfo = new ResourceInfo();
		resourceInfo.setRoleId(getCurRoleId());
		list = this.resourceDao.getResources(resourceInfo);
		actionPageBean = new ActionPageBean<ResourceInfo>();
		actionPageBean.setData(list);
		return "success";
	}

	public String getFirstPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = resourceDao.getAllResourcesCount(curRole);
		actionPageBean = new ActionPageBean<ResourceInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ResourceInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		pageBean.setPrevPageNo(1);
		pageBean.setCurrentPageNo(1);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		if (maxPageNo <= 1) {
			pageBean.setNextPageNo(1);
		} else {
			pageBean.setNextPageNo(2);
		}

		int offset = 0;
		int limit = pageBean.getPageNum();
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<ResourceInfo> data = resourceDao.getAllResources(curRole,
				rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getPrevPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = resourceDao.getAllResourcesCount(curRole);
		actionPageBean = new ActionPageBean<ResourceInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ResourceInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		setPageBean(pageBean);
		List<ResourceInfo> data = resourceDao.getAllResources(curRole,
				rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getNextPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = resourceDao.getAllResourcesCount(curRole);
		actionPageBean = new ActionPageBean<ResourceInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ResourceInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		setPageBean(pageBean);
		List<ResourceInfo> data = resourceDao.getAllResources(curRole,
				rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getLastPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = resourceDao.getAllResourcesCount(curRole);
		actionPageBean = new ActionPageBean<ResourceInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ResourceInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		pageBean.setCurrentPageNo(maxPageNo);
		pageBean.setNextPageNo(maxPageNo);

		if (maxPageNo <= 1) {
			pageBean.setPrevPageNo(1);
		} else {
			pageBean.setPrevPageNo(maxPageNo - 1);
		}
		int limit = pageBean.getPageNum();
		int offset = (maxPageNo - 1) * limit;
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<ResourceInfo> data = resourceDao.getAllResources(curRole,
				rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	private IRoleDao roleDao;

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}
	
	private UserInfo getCurUser(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		Object obj = session.get("user");
		UserInfo result = new UserInfo();
		result.setUserId(-1);
		if(obj == null){
			return result;
		}
		
		if(!(obj instanceof UserInfo)){
			return result;
		}
		
		result = (UserInfo) obj;
		return result;
	}
	
	private RoleInfo getCurRole(){
		RoleInfo result = new RoleInfo();
		result.setRoleId(-1);
		
		UserInfo userInfo = getCurUser();
		List<RoleInfo> roles = userInfo.getRoleInfos();
		
		if(roles == null || roles.isEmpty()){
			return result;
		}
		
		result = roles.get(0);
		return result;
	}
	
	private int getCurRoleId() {
		RoleInfo role = getCurRole();
		int curRoleId = role.getRoleId();
		return curRoleId;
	}

	public String save() throws Exception {
		int curRoleId = getCurRoleId();
		if(curRoleId < 0){
			logger.info(" user not found ! ");
			return ERROR;
		}
		ResourceInfo form = this.getForm();
		if (oper.equals("save")) {
			resourceDao.addResourceInfo(form);
			int resourceId = form.getResId();
			List<RoleInfo> rlist = new ArrayList<RoleInfo>();
			rlist = roleDao.getChildRolesLeaf2Root(curRoleId);
			RoleInfo r = new RoleInfo();
			r.setRoleId(curRoleId);
			List<RoleResource> resourceList = new ArrayList<RoleResource>();
			for (RoleInfo role : rlist) {
				RoleResource rrr = new RoleResource();
				rrr.setResourceId(resourceId);
				rrr.setRoleId(role.getRoleId());
				resourceList.add(rrr);
			}
			this.roleResDao.insertBatch(resourceList);
			resetForm();
			return "success";
		} else {
			resourceDao.updateResource(form);
			resetForm();
			this.resId = 0;
			return "success";
		}
	}

	public String init() throws Exception {

		if (oper.equals("save")) {
			return "new";
		} else {
			ResourceInfo resourrce = resourceDao.getResourceById(resId);
			setForm(resourrce);
			return "update";
		}
	}

	public String delete() throws Exception {
		this.roleResDao.delRoleResByResId(resIds[0]);
		resetForm();
		return "success";
	}

	protected UserInfo getCurrentUser() {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		UserInfo user = (UserInfo) session.get("user");
		return user;
	}

	public void setForm(ResourceInfo res) {
		resId = res.getResId();
		resName = res.getResName();
		// resType=res.getResType();
		resDesc = res.getResDesc();
	}

	public ResourceInfo getForm() {
		ResourceInfo res = new ResourceInfo();
		res.setResDesc(resDesc);
		res.setResId(resId);
		res.setResName(resName);
		res.setResType(1);
		return res;
	}

	public void resetForm() {
		resId = 0;
		resName = "";
		resType = 1;
		resDesc = "";
	}

	public IResourceDao getResourceDao() {
		return resourceDao;
	}

	public void setResourceDao(IResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	public List<ResourceInfo> getList() {
		return list;
	}

	public void setList(List<ResourceInfo> list) {
		this.list = list;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public int getResId() {
		return resId;
	}

	public void setResId(int resId) {
		this.resId = resId;
	}

	public String getResName() {
		return resName;
	}

	public void setResName(String resName) {
		this.resName = resName;
	}

	public int getResType() {
		return resType;
	}

	public void setResType(int resType) {
		this.resType = resType;
	}

	public String getResDesc() {
		return resDesc;
	}

	public void setResDesc(String resDesc) {
		this.resDesc = resDesc;
	}

	public int[] getResIds() {
		return resIds;
	}

	public void setResIds(int[] resIds) {
		this.resIds = resIds;
	}

	public ActionPageBean<ResourceInfo> getActionPageBean() {
		return actionPageBean;
	}

	public void setActionPageBean(ActionPageBean<ResourceInfo> actionPageBean) {
		this.actionPageBean = actionPageBean;
	}

	public ActionPageBean<ResourceInfo> getPageBean() {
		ActionPageBean<ResourceInfo> bean = new ActionPageBean<ResourceInfo>();
		bean.setCurrentPageNo(currentPageNo);
		bean.setPrevPageNo(prevPageNo);
		bean.setNextPageNo(nextPageNo);
		bean.setTotalCount(totalCount);
		bean.setPageNum(pageNum);
		return bean;
	}

	public void setPageBean(ActionPageBean<ResourceInfo> bean) {
		currentPageNo = bean.getCurrentPageNo();
		prevPageNo = bean.getPrevPageNo();
		nextPageNo = bean.getNextPageNo();
		totalCount = bean.getTotalCount();
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getPrevPageNo() {
		return prevPageNo;
	}

	public void setPrevPageNo(int prevPageNo) {
		this.prevPageNo = prevPageNo;
	}

	public int getNextPageNo() {
		return nextPageNo;
	}

	public void setNextPageNo(int nextPageNo) {
		this.nextPageNo = nextPageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public IRoleResDao getRoleResDao() {
		return roleResDao;
	}

	public void setRoleResDao(IRoleResDao roleResDao) {
		this.roleResDao = roleResDao;
	}

}
